<?php

include ("../lib/BLL/DonViBLL.php");
include ("../lib/BLL/DmNganhBLL.php");
include ("../lib/BLL/DonViHocVuBLL.php");
include ("../lib/BLL/DmMonHocBLL.php");
include ("../lib/BLL/DmCaHocBLL.php");
include ("../lib/BLL/KhoaHocBLL.php");
include ("../lib/BLL/LopMonBLL.php");
include ("../lib/BLL/CanBoBLL.php");
include ("../lib/BLL/QuaTrinhLopMonBLL.php");
include ("../lib/BLL/LopMonCTDTBLL.php");
include ("../lib/BLL/MonCTDTBLL.php");
include ("../lib/BLL/DangKyMonBLL.php");
include ("../lib/BLL/SinhVienLopBLL.php");
include ("../lib/BLL/ChiTietLopMonBLL.php");
include ("../lib/BLL/HoiDongThiBLL.php");


$donvi=new DonViBLL();
$nganh=new DmNganhBLL();
$dvhv=new DonViHocVuBLL();
$monhoc= new DmMonHocBLL();
$cahoc= new DmCaHocBLL();
$khoahoc= new KhoaHocBLL();
$canbo= new CanBoBLL();
$lopmonBLL=new LopMonBLL();
$quatrinhlopmonBLL=new QuaTrinhLopMonBLL();
$lopmon_ctdt=new LopMonCTDTBLL();
$mon_ctdt= new MonCTDTBLL();
$dkmBLL=new DangKyMonBLL();
$svlop= new SinhVienLopBLL();
$ctlm=new ChiTietLopMonBLL();
$hoidong=new HoiDongThiBLL();

$lopmonDAL=new LopMon();
$quatrinhlopmonDAL=new QuaTrinhLopMon();
$lopmon_ctdtDAL= new LopMonCTDT();
$dkmDAL=new DangKyMon();
$ctlmDAL= new ChiTietLopMon();
$hoidongDAL=new HoiDongThi();

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
if(isset($_POST['Luu'])){
    
    $khoa_lopmon=date('d/m/Y/H/i/s'); 
    $soluongmon=$_POST['soluongmon'];    
    //mysql_query("DELETE FROM tbl_lopmon WHERE 1");
    //mysql_query("DELETE FROM tbl_quatrinhlopmon WHERE 1");
    //mysql_query("DELETE FROM tbl_lopmon_monctdt WHERE 1");
    //mysql_query("DELETE FROM tbl_dangkymon WHERE 1");
    //mysql_query("DELETE FROM tbl_chitiet_lopmon WHERE 1");
    //mysql_query("DELETE FROM tbl_laplich WHERE 1");
    
    $ma_donvi=$_POST['donviluu'];
    $ma_donvihocvu=$_POST['dvhvluu'];
    
    /*
    $ma_hoidong=$hoidong->Getma_hoidong($ma_donvi,$ma_donvihocvu);
    if($ma_hoidong==Null){
        $ma_hoidong=$ma_donvi.'-'.$ma_donvihocvu;
        $hoidongDAL->sethoidongthi($ma_donvihocvu,$ma_donvi,$ma_hoidong);
        $hoidong->InsertHoidongthi($hoidongDAL);
    }
    */
    for($i=0;$i<$soluongmon;$i++){
        if(isset($_POST['khoa'.$i]) && $_POST['khoa'.$i]){
          
            $ma_lopmon=$khoa_lopmon.$i;                 $ten_lopmon="";
            $ngaybatdau=$_POST['ngay1'.$i];             $ngayketthuc=$_POST['ngay2'.$i];
            $ma_phong="";                               $ma_cbgv=$_POST['canboluu'.$i];
            $ngayduyet="";
            $ngaytao=date('d/m/Y');                     $nguoiduyet=$_SESSION['ma_cb'];
            $nguoitao=$_SESSION['ma_cb'];               $trangthai_hientai="dukien";
            $ngaythi1=$_POST['ngaythi1'.$i];            $cathi1=$_POST['cathiluu1'.$i];
            $ngaythi2=$_POST['ngaythi2'.$i];            $cathi2=$_POST['cathiluu2'.$i];
          
///Lớp môn            
            $lopmonDAL->setLopMon($ma_lopmon,$ten_lopmon,$ngaybatdau,$ngayketthuc,$ma_donvi,$ma_phong,$ma_donvihocvu,
                                  $ma_cbgv,'-',$ngayduyet,$ngaytao,$nguoiduyet,$nguoitao,$trangthai_hientai);
            $kq=$lopmonBLL->insertLopMon($lopmonDAL);
            
//quá trình lớp môn          
            $madm_ttlm='dukien';
            $ma_qtlm=$ma_lopmon."-".$madm_ttlm;
            $noidungthuchien='Tạo lớp môn';
        	$thoigianthuchien=$ngaytao;
            
            $quatrinhlopmonDAL->setQTLopMon($nguoitao,$ma_lopmon,$ma_qtlm,$madm_ttlm,$noidungthuchien,$thoigianthuchien);
            $quatrinhlopmonBLL->insertQTLopMon($quatrinhlopmonDAL);
           
            $monhoccheck=$_POST['monluu'.$i];
            
            $nganhcheck=$_POST['nganhluu'.$i];
            $nganhcheck=rtrim($nganhcheck,',');
            $arr_nganh=explode(',',$nganhcheck);
            $songanh=count($arr_nganh);
            for($j=0;$j<$songanh;$j++){
                $ma_mon_ctdt=$mon_ctdt->getMonCTDT($monhoccheck,$arr_nganh[$j]);
                $lopmon_ctdtDAL->setLopMonCTDT($ma_lopmon,$ma_mon_ctdt);
                $kq=$lopmon_ctdt->insertLopMonCTDT($lopmon_ctdtDAL);
            }
            
            $malop=rtrim($_POST['lopluu'.$i],',');
            $arr=$svlop->getsv_arrlop($malop);
            
            foreach($arr as $key=>$values){
                    $ma_dkm=$ma_lopmon."-".$values;
                    $dkmDAL->setDangKyMon($ma_dkm,$ma_lopmon,$values,'dukien');
                    $dkmBLL->insertDangKyMon($dkmDAL);                    
                }  
            
            $malop=str_replace("'","",$malop);
            $arr_lophc=explode(',',$malop);
                                            
            foreach($arr_lophc as $key=>$lophc){
                $ma_qtlm=$ma_lopmon."-".$lophc;
                $quatrinhlopmonDAL->setQTLopMon($nguoitao,$ma_lopmon,$ma_qtlm,'lophc',$lophc,$thoigianthuchien);
                $quatrinhlopmonBLL->insertQTLopMon($quatrinhlopmonDAL);
            }
                
//Chi tiết lớp môn                
            $ma_ctlm=$ma_lopmon."hoc1";
            $ctlmDAL->setChiTietLopMon("",$ma_cbgv,$ma_ctlm,$ma_lopmon,"",'-','dukienhoc',$ngaybatdau,$nguoiduyet,$ngaytao);
            $ctlm->Insert_ChiTietLopMon($ctlmDAL);
            
            $ma_ctlm=$ma_lopmon."hoc2";
            $ctlmDAL->setChiTietLopMon("",$ma_cbgv,$ma_ctlm,$ma_lopmon,"",'-','dukienhoc',$ngayketthuc,$nguoiduyet,$ngaytao);
            $ctlm->Insert_ChiTietLopMon($ctlmDAL);
            
            $ma_ctlm=$ma_lopmon."thi1";
            $ctlmDAL->setChiTietLopMon("",$ma_cbgv,$ma_ctlm,$ma_lopmon,"",$cathi1,'thilan1',$ngaythi1,$nguoiduyet,$ngaytao);
            $ctlm->Insert_ChiTietLopMon($ctlmDAL);
            
            $ma_ctlm=$ma_lopmon."thi2";
            $ctlmDAL->setChiTietLopMon("",$ma_cbgv,$ma_ctlm,$ma_lopmon,"",$cathi2,'thilan2',$ngaythi2,$nguoiduyet,$ngaytao);
            $ctlm->Insert_ChiTietLopMon($ctlmDAL);
            
            
        }//END IF $_POST['KHOA']
    }
                
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

$ds_donvi=$donvi->getListDonVi_Lienket();
$smarty->assign('ds_donvi',$ds_donvi);

$ds_nganh= $nganh->getListNganh();
$smarty->assign('ds_nganh',$ds_nganh);

$so_nganh= $nganh->countNganh();
$smarty->assign('so_nganh',$so_nganh);

$ds_dvhv= $dvhv->getListDonViHocVu();
$smarty->assign('ds_dvhv',$ds_dvhv);

$ds_monhoc=$monhoc->getListDmMonHoc();
$smarty->assign('ds_monhoc',$ds_monhoc);

$ds_cahoc=$cahoc->getListDmCaHoc();
$smarty->assign('ds_cahoc',$ds_cahoc);

$ds_namkhoahoc=$khoahoc->getListNamKhoaHoc();
$smarty->assign('ds_namkhoahoc',$ds_namkhoahoc);

$ds_canbo=$canbo->getListCanBo();
$smarty->assign('ds_canbo',$ds_canbo);


$smarty->display('daotao/lapkehoach.tpl');   
?>